package com.tools.scanner.utils

import android.app.Activity
import android.content.Context
import android.graphics.Point
import android.graphics.Rect
import android.view.WindowManager
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.tools.scanner.QrApp

object DisplayUtil {

    fun getWindowScreen(): Point {
        val wm = QrApp.app.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        val display = wm.defaultDisplay
        val screenResolution = Point()
        display.getSize(screenResolution)
        return screenResolution
    }

    fun getStatusBarHeightStandard(activity: Activity): Int {
        val window = activity.window ?: return 0
        val decorView = window.decorView
        return ViewCompat.getRootWindowInsets(decorView)
            ?.getInsets(WindowInsetsCompat.Type.statusBars())?.top ?: run {
            getStatusBarHeightLegacy(activity)
        }
    }

    private fun getStatusBarHeightLegacy(activity: Activity): Int {
        val window = activity.window ?: return 0
        val rect = Rect()
        window.decorView.getWindowVisibleDisplayFrame(rect)
        return rect.top.takeIf { it > 0 } ?: run {
            activity.resources.getDimensionPixelSize(
                activity.resources.getIdentifier("status_bar_height", "dimen", "android")
            )
        }
    }

}